package io.github.springsongs.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.github.springsongs.service.PaymentHystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@DefaultProperties(defaultFallback = "parment_Global_FallbackMethod")
public class OrderHystrixController {


    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @Value("${server.port}")
    public String serverPort;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_Ok(id);
        return result;
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })//服务降级，兜底方法,超时上限3秒

    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id")Integer id){
        String result=paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    public String paymentInfo_TimeOutHandler(Integer id){
        return "销费端，对方系数繁忙或者自个出错！ paymentInfo_TimeOutHandler,Id: "+id+"/t 难过";
    }

    public String parment_Global_FallbackMethod(){
        return "全局异常";
    }

}
